﻿@model Jb.Web.ViewModels.UtilityModels.ListConfig
@using Jb.Common.Extensions;
@using Jb.Web.Utility;
@{    
	String pageTitle = "Contact List";
    ViewBag.Title = Html.GetPageTitle(pageTitle);
	String editUrlFormat = Url.PrivateUrl("Edit", "Contact") + "/{0}";
	///Bind Contact ListConfig
	///Following ListConfig binding needs to be edited according to the query result and need.
//    Jb.Web.ViewModels.UtilityModels.ListConfig _ListConfig = Jb.Web.ViewModels.UtilityModels.ListConfig.Build(
//        @"CompanyID {CompanyID}				
//		, Name {Name}				
//		, Gender {Gender}				
//		, DateOfBirth {DateOfBirth}				
//		, IsActive {IsActive}"
//        , Model.DataSource, true, editUrlFormat);
    if( Model.Pager != null){
        Model.Pager.UrlFormat = "/Private/Contact/Edit/{0}";
    }
    ViewData[AppConstants.ViewDataKeys.SELECTED_MENU] = EmployerMenu.ManageContacts;
}
<div class="jb-page-heading">
    <h1>@pageTitle</h1>
    <div>
        Add, Edit and manage Contact.
    </div>
</div>
<div class="jb-page-content">    
    <a href="@Url.PrivateUrl("Edit", "Contact")">Add New Contact</a>

    <table class="table" cellpadding="3" cellspacing="0">
        <tr>            
            <th> Name </th>
            <th> Gender </th>
            <th> Date Of Birth </th>
            <th> IsActive </th>
            <th style="text-align:center;">Action</th>
            <th style="text-align:center;"> Manage User </th>
        </tr>
        @foreach (System.Data.DataRow _Row in Model.DataSource.Rows)
        {
            int _ContactID = Jb.Common.DB.NullHandler.GetInt(_Row["ID"]);
            int _UserID = Jb.Common.DB.NullHandler.GetInt(_Row["UserID"]);
            String _UserName = Jb.Common.DB.NullHandler.GetString(_Row["UserName"]);
            String _EditUserText = _UserID == 0 ? "Create User" : "Edit User";
        <tr>
            <td>@WebUtil.FormatData(_Row["Name"])</td>
            <td>@WebUtil.FormatData(_Row["Gender"])</td>
            <td>@WebUtil.FormatData(_Row["DateOfBirth"])</td>
            <td>@WebUtil.FormatData(_Row["IsActive"])</td>
            <td style="text-align:center;">
                <a href="@Url.PrivateUrl("Edit", "Contact")/@_ContactID">Edit</a>
            </td>
            <td style="text-align:center;"><a href="javascript:void(0);" onclick="@String.Format("ShowCreateUserDialog({0}, {1}, '{2}')", _ContactID, _UserID, _UserName)">@_EditUserText</a></td>
        </tr>
        }
    </table>

    @*@Html.Partial("_ListView", _ListConfig)*@
    @if (Model.Pager != null)
    {    
        @Html.Partial("_Pager", Model.Pager)
    }
</div>


<!--Modal Popup Section Start-->
<div id="divConfirmDialog" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="divPhotoEditModalLabel" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">X</button>
        <h4>Create User</h4>
    </div>
    <div class="modal-body jb-inline-label">          
        <form id="frmCreateUser" method="post" action="/Private/Contact/CreateUserFromContact">            
            <div class="editor-label">
                <label for="Email">User Name</label>
            </div>
            <div class="editor-field">            
                <input data-val="true" data-val-required="Please enter an User Name"
                    data-val-length="The User Name must be at least 6 - 10 characters long." 
                    data-val-length-max="10" data-val-length-min="6"
                    id="UserName" name="UserName" type="text" value="" />
                <span class="field-validation-valid" data-valmsg-for="UserName" data-valmsg-replace="true"></span>
            </div>

            <div class="editor-label">
                <label for="Password">Password</label>
            </div>
            <div class="editor-field">
                <input data-val="true" data-val-length="The Password must be at least 6 characters long." 
                    data-val-length-max="100" data-val-length-min="6" 
                    data-val-required="Please Enter Password."
                    class="text-box single-line password" id="Password" name="Password" type="password" value="" />            
                <span class="field-validation-valid" data-valmsg-for="Password" data-valmsg-replace="true"></span>
            </div>
            <div class="editor-label">
                <label for="ConfirmPassword">Confirm password</label>
            </div>
            <div class="editor-field">
                <input
                    data-val="true" 
                    data-val-equalto="The password and Confirm password do not match." 
                    data-val-required="Please Confirm your Password."
                    data-val-equalto-other="*.Password"
                    id="ConfirmPassword" name="ConfirmPassword" type="password" value="" />
                <span class="field-validation-valid" data-valmsg-for="ConfirmPassword" data-valmsg-replace="true"></span>
            </div>
            <div id="divSuccessMessage" style="display:none;"></div>            
        </form>
    </div>
    <div class="modal-footer">               
        <button class="btn btn-primary" onclick="SaveContactUser();"><i class="icon-hdd icon-white"></i> Save</button>
        <button class="btn" data-dismiss="modal" aria-hidden="true"><i class="icon-off"></i> Cancel</button>
    </div>
</div>
<!-- Modal Popup Section End -->
        

@section JavaScriptPlaceHolder
{
    @Html.JavaScript("jquery.validate.min.js", Url)
    @Html.JavaScript("jquery.validate.unobtrusive.min.js", Url)

    <script language="javascript" type="text/javascript">
        var _ContactID = 0, _UserID = 0, _UserName = '';
        function ShowCreateUserDialog(contactID, userID, userName) {
            _ContactID = contactID;
            _UserID = parseInt(userID);
            _UserName = userName;
            $('#UserName').val(_UserName); $('#Password').val(''); $('#ConfirmPassword').val('');            
            $('#divSuccessMessage').html('').hide();            
            $('#divConfirmDialog').modal({ show: true });
        }

        function SaveContactUser() {
            if ($('#frmCreateUser').valid()) {
                Ajax('@Url.Action("CreateUserFromContact", "Contact", new {area=AppConstants.Area.PRIVATE})',
                'UserName=' + $('#UserName').val() + '&password=' + $('#Password').val() + '&contactId=' + _ContactID + '&userid=' + _UserID,
                function (result) {
                    if (result == -1)
                        $('#divSuccessMessage').html(WarningMessageBox('Sorry! the User Name already exists, please choose another.')).show();                        
                    else if (result == 1)
                        $('#divSuccessMessage').html(SuccessMessageBox('Congratulations! User Information saved successfully.')).show();                        
                });
            }
        }
    </script>
}

    


